<?php
namespace model;

/**
 * Description of Criteria
 *
 * @author Damian
 */
class Criteria extends \library\Base{
     /**
     * <p>obsługa zapytań do bazy danych</p>
     * @var database\Query
     */
    protected $query = null;
    /**
     *lista połączonych tabel z bazy danych
     * @readwrite
     */
    public $joinedTables;
    /**
     *lista klas połączonych modeli
     * @var array
     */
    public $joinedClasses;
    public $countTables=0;
    /**
     * @readwrite
     * @var \model\database\PDOConnector 
     */
    protected $connector = null;
    /**
     * <p>zwraca konektor do bazy danych</p>
     * @return \model\database\PDOConnector 
     * @throws \library\exceptions\model\ExceptionConnectorNotFound
     */
    public function getConnector() {
        if (empty($this->connector)) {
            $database = \library\Registry::get("database");
            if (!$database) {
                throw new \library\exceptions\model\ExceptionConnectorNotFound();
            }
            $this->connector = $database->initialize();
        }
        return $this->connector;
    }
    /**
     * ustawia warunek na pobranie modelu
     * @param type $pole <p>nazwa kolumny z tabeli</p>
     * @param type $operator <p> dozwolone wartośći "=", "<", ">", "like"</p>
     * @param type $value <p> warunek do sprawdzenia</p>
     * @param mixed $table [optional = false] <p>$table - nazwa tabeli z której jest dane pole</p>
     * @param mixed $connector [optional = " "] <p>sposób łaczenia warunków, dozwolone wartości to and,or,spacja</p>
     * @param boolean $quote [optional = true] <p>określa czy pole jest kolumną (true) czy wyrażeniem (false)</p>
     * @return \model\Criteria
     */
    public function where($pole, $operator, $value, $table=false,$connector=" ",$quote=true) {
        $this->getConnector();
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        if (is_string($table)) {
            $this->query->where("%s`{$table}`.`{$pole}` {$operator} ?",$connector, $value);
        }
        else {
            if ($quote){
                $this->query->where("%s`{$pole}` {$operator} ?",$connector, $value);
            }
            else {
                $this->query->where("%s{$pole} {$operator} ?",$connector, $value);
            }
            
        }
        $this->connector = null;
        return $this;
    }
    /**
     * 
     * @param mixed $model<p> jeśli model jest instancją klasy ModelBase nazwa tabei jest pobierana z modelu,<br/>
     *  jeśli jest stringiem to jest to nazwa tabeli </p>
     * @param type $fields [optional] <p>lista kolumn do pobrania, lub string all</p>
     * @return \model\Criteria
     */
    public function from($model, $fields="all"){
        $this->countTables++;
        $this->getConnector();
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        $this->joinedTables[]=$model;
        if (is_string($fields)){
            $class = "\model\\".ucfirst($model);
            $temp = new $class();
            $this->query->from($model, $temp->getColumnNames());
        }
        else {
           // $this->joinedTables[]=$model;
            $this->query->from($model, $fields);
        }
         
        return $this;
    }
    public function order($order = null, $direction = null, $table=false) {
        $this->getConnector();
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        if (is_string($table)) {
            $this->query->order("`".$table."`.`".$order."`", $direction);
        }
        else {
            $this->query->order($order, $direction);
        }
        
        $this->connector = null;
        return $this;
    }
    public function join(Model $model,$columns="all",$type="LEFT JOIN"){        
       $this->getConnector();
       $table=$model->getTable();
       $model->getColumns();
       if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        $relation = $model->getRelation("`".$this->joinedTables[0]."`", "`".$table."`");
        if (!$relation && sizeof($this->query->joinedTables)>1){
            for ($i=1;$i<sizeof($this->query->joinedTables);$i++){
                $relation = $model->getRelation("`".$this->query->joinedTables[$i]."`", "`".$table."`");
            }
        }
        if ($relation){
            $this->countTables++;
            $this->joinedTables[]=$model->getTable();
            $this->query->join($model->getTable(), $relation, strtoupper($type),  $model->getColumnNames($columns));
        }
        return $this;
        
    }
     public function limit($limit = null, $page = 1) {
        $this->getConnector();
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        $this->query->setLimit($limit, $page);
        $this->connector = null;
        return $this;
    }
    public function all(){
        return $this->query->all();
    }
    public function delete(){
        return $this->query->delete();
    }
    public function first(){
        return $this->query->first();
    }

    public function count(){
        return $this->query->count();
    }

    public function in($pole, array $vaules,$table=false,$connector=" ",$quote=false) {
        $this->getConnector();
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        $this->query->quote = false;
        $this->where($pole, "in", "(" . implode(",", \library\ArrayMethods::escape($vaules)) . ")",$table,$connector,$quote);
        $this->query->quote = true;
        return $this;
    }
    public function getSelectQuery(){
        if ($this->query == null) {
            $this->query = $this->connector->query();
        }
        return $this->query->getSelectQuery();
    }
}
